package com.meusconcursos.dataupdate;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.meusconcursos.dataupdate.tables.TableCargo;
import com.meusconcursos.dataupdate.tables.TableConteudoProgramatico;
import com.meusconcursos.dataupdate.tables.TableEdital;
import com.meusconcursos.dataupdate.tables.TableFormacao;
import com.meusconcursos.dataupdate.tables.TableMateria;
import com.meusconcursos.dataupdate.tables.TableNivelCargo;
import com.meusconcursos.dataupdate.tables.TablePlanoEstudo;
import com.meusconcursos.dataupdate.tables.TableProva;
import com.meusconcursos.dataupdate.tables.TableTipoProva;
import com.meusconcursos.dataupdate.tables.TableTipoProvaMateria;
import com.meusconcursos.dataupdate.tables.TableUnidadeEstudo;

public class DataHandler extends DefaultHandler {
	private List<Table> tables;
	private String idEdital = null;

	public List<Table> getTables() {
		return tables;
	}

	private Table currentTable;
	private StringBuilder builder;

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		super.characters(ch, start, length);
		builder.append(ch, start, length);
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		super.endElement(uri, localName, qName);

		if (localName.equalsIgnoreCase("table")) {
			if (this.currentTable != null) {
				tables.add(currentTable);
				currentTable = null;
			}
		}
		builder.setLength(0);

	}

	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
		tables = new ArrayList<Table>();
		builder = new StringBuilder();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		String nomeTabela = null;
		if (localName.equalsIgnoreCase("table")) {

			nomeTabela = attributes.getValue("nome");

			if (nomeTabela.equalsIgnoreCase("edital")) {
				currentTable = new TableEdital();
			} else if (nomeTabela.equalsIgnoreCase("nivel_cargo")) {
				currentTable = new TableNivelCargo();
			} else if (nomeTabela.equalsIgnoreCase("cargo")) {
				currentTable = new TableCargo();
			} else if (nomeTabela.equalsIgnoreCase("prova")) {
				currentTable = new TableProva();
			} else if (nomeTabela.equalsIgnoreCase("materia")) {
				currentTable = new TableMateria();
			} else if (nomeTabela.equalsIgnoreCase("conteudoprogramatico")) {
				currentTable = new TableConteudoProgramatico();
			} else if (nomeTabela.equalsIgnoreCase("formacao")) {
				currentTable = new TableFormacao();
			} else if (nomeTabela.equalsIgnoreCase("tipoprova_materia")) {
				currentTable = new TableTipoProvaMateria();
			} else if (nomeTabela.equalsIgnoreCase("planoestudo")) {
				currentTable = new TablePlanoEstudo();
			} else if (nomeTabela.equalsIgnoreCase("unidadeestudo")) {
				currentTable = new TableUnidadeEstudo();
			} else if (nomeTabela.equalsIgnoreCase("tipoprova")) {
				currentTable = new TableTipoProva();
			}
		} else if (localName.equalsIgnoreCase("record") && currentTable != null) {

			if (currentTable instanceof TableEdital) {

				if (idEdital == null) {
					idEdital = attributes.getValue("_id");
				}

				currentTable.add(currentTable.newRecordInstance(new String[] {
						attributes.getValue("_id"),
						attributes.getValue("numero"),
						attributes.getValue("ano"),
						attributes.getValue("sigla_orgao"),
						attributes.getValue("nome_orgao") }));
			} else if (currentTable instanceof TableNivelCargo) {
				currentTable
						.add(currentTable.newRecordInstance(new String[] {
								attributes.getValue("_id"),
								attributes.getValue("nome") }));
			} else if (currentTable instanceof TableCargo) {
				currentTable.add(currentTable.newRecordInstance(new String[] {
						attributes.getValue("_id"), idEdital,
						attributes.getValue("id_nivel_cargo"),
						attributes.getValue("nome") }));
			} else if (currentTable instanceof TableProva) {
				currentTable.add(currentTable.newRecordInstance(new String[] {
						attributes.getValue("_id"),
						attributes.getValue("id_formacao"),
						attributes.getValue("id_tipoprova") }));
			} else if (currentTable instanceof TableMateria) {
				currentTable.add(currentTable.newRecordInstance(new String[] {
						attributes.getValue("_id"), idEdital,
						attributes.getValue("nome") }));
			} else if (currentTable instanceof TableConteudoProgramatico) {
				currentTable.add(currentTable.newRecordInstance(new String[] {
						attributes.getValue("id_materia"),
						attributes.getValue("descricao") }));
			} else if (currentTable instanceof TableFormacao) {
				currentTable.add(currentTable.newRecordInstance(new String[] {
						attributes.getValue("_id"),
						attributes.getValue("id_cargo"),
						attributes.getValue("nome"),
						attributes.getValue("requisitos"),
						attributes.getValue("atribuicoes"),
						attributes.getValue("remuneracao"),
						attributes.getValue("jornada_diaria"),
						attributes.getValue("carga_horaria") }));
			} else if (currentTable instanceof TableTipoProva) {
				currentTable.add(currentTable.newRecordInstance(new String[] {
						attributes.getValue("_id"),
						attributes.getValue("id_cargo"),
						attributes.getValue("nome") }));
			} else if (currentTable instanceof TableTipoProvaMateria) {
				currentTable.add(currentTable.newRecordInstance(new String[] {
						attributes.getValue("id_tipoprova"),
						attributes.getValue("id_materia"),
						attributes.getValue("peso"),
						attributes.getValue("numero_questoes") }));
			} else if (currentTable instanceof TablePlanoEstudo) {
				currentTable.add(currentTable
						.newRecordInstance(new String[] { attributes
								.getValue("id_formacao") }));
			} else if (currentTable instanceof TableUnidadeEstudo) {
				currentTable.add(currentTable.newRecordInstance(new String[] {
						attributes.getValue("id_planoestudo"),
						attributes.getValue("id_conteudoprogramatico") }));
			}
		}
	}

	@Override
	public void endDocument() throws SAXException {
		super.endDocument();

		Iterator<Table> itTables = tables.iterator();
		while (itTables.hasNext()) {
			Table currentTable = (Table) itTables.next();
			Iterator<Record> itRecords = currentTable.getRecords().iterator();
			while (itRecords.hasNext()) {
				Record record = (Record) itRecords.next();
				currentTable.inserir(record.getCv());
			}
		}

	}

}
